package com.lyp.adapters;

import android.content.Context;
import android.os.Handler;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.afollestad.materialdialogs.MaterialDialog;
import com.blankj.utilcode.util.ToastUtils;
import com.lyp.jsonbean.CarReceBean;
import com.lyp.jsonbean.JLCarBean;
import com.lyp.jsonbean.LoginBean;
import com.tjl.yangxixis.R;
import com.tjl.yangxixis.api.YangxixiApi;

import java.io.IOException;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * 作者:ChenXi 时间:2017/5/31 14: 26
 */
//接单(车内)
public class SingeCarAdapter extends RecyclerView.Adapter<SingeCarAdapter.MyViewHolder>{

    private List<JLCarBean.DataBean> mList;
    private SingeCarAdapter.MyItemClickListener mListener;
    private Context mContext;
    private LoginBean.DataBean mLoginbean;

    public SingeCarAdapter(List<JLCarBean.DataBean> list,Context context,LoginBean.DataBean loginbean) {
        mList = list;
        mContext = context;
        mLoginbean = loginbean;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        SingeCarAdapter.MyViewHolder holder = new SingeCarAdapter.MyViewHolder(
                LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.fragment_singe_item, viewGroup,
                false));
        return holder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder myViewHolder, final int i) {
        myViewHolder.mType.setText(mList.get(i).getServer_type());//类型
        myViewHolder.mTime.setText(mList.get(i).getExpected_time());//期望时间
        myViewHolder.mMotorcycle.setText(mList.get(i).getShop_car_type());//车型
        myViewHolder.mCarnumber.setText(mList.get(i).getLicense_number());//车牌
        myViewHolder.mSingeRegion.setVisibility(View.GONE);
        myViewHolder.mSingeCar.setVisibility(View.VISIBLE);
        myViewHolder.mBaojia.setVisibility(View.GONE);
        myViewHolder.mGrab.setVisibility(View.GONE);
        myViewHolder.mJoin.setVisibility(View.VISIBLE);
        myViewHolder.mJoin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    CarRece(mList.get(i).getO_id(),mLoginbean.getC_id(), i);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    /**
     * 设置Item点击监听
     * @param listener
     */
    public void setOnItemClickListener(SingeCarAdapter.MyItemClickListener listener){
        this.mListener = listener;
    }

    public class MyViewHolder extends RecyclerView.ViewHolder{

        TextView mType;
        TextView mTime;
        TextView mMotorcycle;//车型
        TextView mCarnumber;//车牌
        Button mJoin,mGrab;
        RelativeLayout mSingeRegion,mSingeCar,mBaojia,mSingeAll;

        public MyViewHolder(View view)
        {
            super(view);
            mType = (TextView) view.findViewById(R.id.tv_server_type);
            mTime = (TextView) view.findViewById(R.id.tv_qwtime);
            mGrab = (Button) view.findViewById(R.id.btn_grab_single);//抢单
            mJoin = (Button) view.findViewById(R.id.btn_join_single);//接单
            mMotorcycle = (TextView) view.findViewById(R.id.tv_carxing);
            mCarnumber = (TextView) view.findViewById(R.id.tv_carnumber);
            mSingeRegion = (RelativeLayout) view.findViewById(R.id.rl_singe_region);//地区
            mSingeCar = (RelativeLayout) view.findViewById(R.id.rl_singe_car);
            mBaojia = (RelativeLayout) view.findViewById(R.id.rl_baojia);//报价
            mSingeAll = (RelativeLayout) view.findViewById(R.id.rl_singeAll);
            mSingeAll.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mListener.onItemClick(v,getAdapterPosition());
                }
            });
        }
    }

    /**
     * item点击接口
     */
    public interface MyItemClickListener {
        void onItemClick(View v, int position);
    }

    //车内接单
    public void CarRece(String o_id,String c_id, final int i) throws IOException {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(YangxixiApi.APP_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        YangxixiApi api = retrofit.create(YangxixiApi.class);
        Call<CarReceBean> call = api.getCarRece(o_id,c_id);
        call.enqueue(new Callback<CarReceBean>() {
            @Override
            public void onResponse(Call<CarReceBean> call, final Response<CarReceBean> response) {
                if (response.body().getResult() == 1 ) {
                    new Handler().post(new Runnable() {
                        @Override
                        public void run() {
                            new MaterialDialog.Builder(mContext)
                                    .title("接单成功")
                                    .content("快去未分配任务进行分配!")
                                    .positiveText("确定")
                                    .canceledOnTouchOutside(false)
                                    .cancelable(false)
                                    .show();
                            mList.remove(i);
                            SingeCarAdapter.this.notifyDataSetChanged();
                                    }
                            });
                     }
                else {
                    new Handler().post(new Runnable() {
                        @Override
                        public void run() {
                            ToastUtils.showShort(response.body().getMessage());
                        }
                    });
                }
            }
            @Override
            public void onFailure(Call<CarReceBean> call, Throwable t) {
                ToastUtils.showShort(t.getMessage());
            }
        });
    }
}
